<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>17.8. readline — GNU readline interface &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="17. Optional Operating System Services" href="someos.html" />
    <link rel="next" title="17.9. rlcompleter — Completion function for GNU readline" href="rlcompleter.html" />
    <link rel="prev" title="17.7. mmap — Memory-mapped file support" href="mmap.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="rlcompleter.html" title="17.9. rlcompleter — Completion function for GNU readline"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mmap.html" title="17.7. mmap — Memory-mapped file support"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="someos.html" accesskey="U">17. Optional Operating System Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-readline">
<h1>17.8. <tt class="xref docutils literal"><span class="pre">readline</span></tt> &#8212; GNU readline interface<a class="headerlink" href="#module-readline" title="Permalink to this headline">¶</a></h1>
<p><em>Platforms: </em>Unix</p>
<p>The <tt class="xref docutils literal"><span class="pre">readline</span></tt> module defines a number of functions to facilitate
completion and reading/writing of history files from the Python interpreter.
This module can be used directly or via the <a title="Python identifier completion, suitable for the GNU readline library." class="reference external" href="rlcompleter.html#module-rlcompleter"><tt class="xref docutils literal"><span class="pre">rlcompleter</span></tt></a> module.  Settings
made using  this module affect the behaviour of both the interpreter&#8217;s
interactive prompt  and the prompts offered by the <a title="raw_input" class="reference external" href="functions.html#raw_input"><tt class="xref docutils literal"><span class="pre">raw_input()</span></tt></a> and
<a title="input" class="reference external" href="functions.html#input"><tt class="xref docutils literal"><span class="pre">input()</span></tt></a> built-in functions.</p>
<p>The <tt class="xref docutils literal"><span class="pre">readline</span></tt> module defines the following functions:</p>
<dl class="function">
<dt id="readline.parse_and_bind">
<tt class="descclassname">readline.</tt><tt class="descname">parse_and_bind</tt><big>(</big><em>string</em><big>)</big><a class="headerlink" href="#readline.parse_and_bind" title="Permalink to this definition">¶</a></dt>
<dd>Parse and execute single line of a readline init file.</dd></dl>

<dl class="function">
<dt id="readline.get_line_buffer">
<tt class="descclassname">readline.</tt><tt class="descname">get_line_buffer</tt><big>(</big><big>)</big><a class="headerlink" href="#readline.get_line_buffer" title="Permalink to this definition">¶</a></dt>
<dd>Return the current contents of the line buffer.</dd></dl>

<dl class="function">
<dt id="readline.insert_text">
<tt class="descclassname">readline.</tt><tt class="descname">insert_text</tt><big>(</big><em>string</em><big>)</big><a class="headerlink" href="#readline.insert_text" title="Permalink to this definition">¶</a></dt>
<dd>Insert text into the command line.</dd></dl>

<dl class="function">
<dt id="readline.read_init_file">
<tt class="descclassname">readline.</tt><tt class="descname">read_init_file</tt><big>(</big><span class="optional">[</span><em>filename</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#readline.read_init_file" title="Permalink to this definition">¶</a></dt>
<dd>Parse a readline initialization file. The default filename is the last filename
used.</dd></dl>

<dl class="function">
<dt id="readline.read_history_file">
<tt class="descclassname">readline.</tt><tt class="descname">read_history_file</tt><big>(</big><span class="optional">[</span><em>filename</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#readline.read_history_file" title="Permalink to this definition">¶</a></dt>
<dd>Load a readline history file. The default filename is <tt class="docutils literal"><span class="pre">~/.history</span></tt>.</dd></dl>

<dl class="function">
<dt id="readline.write_history_file">
<tt class="descclassname">readline.</tt><tt class="descname">write_history_file</tt><big>(</big><span class="optional">[</span><em>filename</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#readline.write_history_file" title="Permalink to this definition">¶</a></dt>
<dd>Save a readline history file. The default filename is <tt class="docutils literal"><span class="pre">~/.history</span></tt>.</dd></dl>

<dl class="function">
<dt id="readline.clear_history">
<tt class="descclassname">readline.</tt><tt class="descname">clear_history</tt><big>(</big><big>)</big><a class="headerlink" href="#readline.clear_history" title="Permalink to this definition">¶</a></dt>
<dd><p>Clear the current history.  (Note: this function is not available if the
installed version of GNU readline doesn&#8217;t support it.)</p>
<p>
<span class="versionmodified">New in version 2.4.</span></p>
</dd></dl>

<dl class="function">
<dt id="readline.get_history_length">
<tt class="descclassname">readline.</tt><tt class="descname">get_history_length</tt><big>(</big><big>)</big><a class="headerlink" href="#readline.get_history_length" title="Permalink to this definition">¶</a></dt>
<dd>Return the desired length of the history file.  Negative values imply unlimited
history file size.</dd></dl>

<dl class="function">
<dt id="readline.set_history_length">
<tt class="descclassname">readline.</tt><tt class="descname">set_history_length</tt><big>(</big><em>length</em><big>)</big><a class="headerlink" href="#readline.set_history_length" title="Permalink to this definition">¶</a></dt>
<dd>Set the number of lines to save in the history file. <a title="readline.write_history_file" class="reference internal" href="#readline.write_history_file"><tt class="xref docutils literal"><span class="pre">write_history_file()</span></tt></a>
uses this value to truncate the history file when saving.  Negative values imply
unlimited history file size.</dd></dl>

<dl class="function">
<dt id="readline.get_current_history_length">
<tt class="descclassname">readline.</tt><tt class="descname">get_current_history_length</tt><big>(</big><big>)</big><a class="headerlink" href="#readline.get_current_history_length" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the number of lines currently in the history.  (This is different from
<a title="readline.get_history_length" class="reference internal" href="#readline.get_history_length"><tt class="xref docutils literal"><span class="pre">get_history_length()</span></tt></a>, which returns the maximum number of lines that will
be written to a history file.)</p>
<p>
<span class="versionmodified">New in version 2.3.</span></p>
</dd></dl>

<dl class="function">
<dt id="readline.get_history_item">
<tt class="descclassname">readline.</tt><tt class="descname">get_history_item</tt><big>(</big><em>index</em><big>)</big><a class="headerlink" href="#readline.get_history_item" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the current contents of history item at <em>index</em>.</p>
<p>
<span class="versionmodified">New in version 2.3.</span></p>
</dd></dl>

<dl class="function">
<dt id="readline.remove_history_item">
<tt class="descclassname">readline.</tt><tt class="descname">remove_history_item</tt><big>(</big><em>pos</em><big>)</big><a class="headerlink" href="#readline.remove_history_item" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove history item specified by its position from the history.</p>
<p>
<span class="versionmodified">New in version 2.4.</span></p>
</dd></dl>

<dl class="function">
<dt id="readline.replace_history_item">
<tt class="descclassname">readline.</tt><tt class="descname">replace_history_item</tt><big>(</big><em>pos</em>, <em>line</em><big>)</big><a class="headerlink" href="#readline.replace_history_item" title="Permalink to this definition">¶</a></dt>
<dd><p>Replace history item specified by its position with the given line.</p>
<p>
<span class="versionmodified">New in version 2.4.</span></p>
</dd></dl>

<dl class="function">
<dt id="readline.redisplay">
<tt class="descclassname">readline.</tt><tt class="descname">redisplay</tt><big>(</big><big>)</big><a class="headerlink" href="#readline.redisplay" title="Permalink to this definition">¶</a></dt>
<dd><p>Change what&#8217;s displayed on the screen to reflect the current contents of the
line buffer.</p>
<p>
<span class="versionmodified">New in version 2.3.</span></p>
</dd></dl>

<dl class="function">
<dt id="readline.set_startup_hook">
<tt class="descclassname">readline.</tt><tt class="descname">set_startup_hook</tt><big>(</big><span class="optional">[</span><em>function</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#readline.set_startup_hook" title="Permalink to this definition">¶</a></dt>
<dd>Set or remove the startup_hook function.  If <em>function</em> is specified, it will be
used as the new startup_hook function; if omitted or <tt class="xref docutils literal"><span class="pre">None</span></tt>, any hook function
already installed is removed.  The startup_hook function is called with no
arguments just before readline prints the first prompt.</dd></dl>

<dl class="function">
<dt id="readline.set_pre_input_hook">
<tt class="descclassname">readline.</tt><tt class="descname">set_pre_input_hook</tt><big>(</big><span class="optional">[</span><em>function</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#readline.set_pre_input_hook" title="Permalink to this definition">¶</a></dt>
<dd>Set or remove the pre_input_hook function.  If <em>function</em> is specified, it will
be used as the new pre_input_hook function; if omitted or <tt class="xref docutils literal"><span class="pre">None</span></tt>, any hook
function already installed is removed.  The pre_input_hook function is called
with no arguments after the first prompt has been printed and just before
readline starts reading input characters.</dd></dl>

<dl class="function">
<dt id="readline.set_completer">
<tt class="descclassname">readline.</tt><tt class="descname">set_completer</tt><big>(</big><span class="optional">[</span><em>function</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#readline.set_completer" title="Permalink to this definition">¶</a></dt>
<dd>Set or remove the completer function.  If <em>function</em> is specified, it will be
used as the new completer function; if omitted or <tt class="xref docutils literal"><span class="pre">None</span></tt>, any completer
function already installed is removed.  The completer function is called as
<tt class="docutils literal"><span class="pre">function(text,</span> <span class="pre">state)</span></tt>, for <em>state</em> in <tt class="docutils literal"><span class="pre">0</span></tt>, <tt class="docutils literal"><span class="pre">1</span></tt>, <tt class="docutils literal"><span class="pre">2</span></tt>, ..., until it
returns a non-string value.  It should return the next possible completion
starting with <em>text</em>.</dd></dl>

<dl class="function">
<dt id="readline.get_completer">
<tt class="descclassname">readline.</tt><tt class="descname">get_completer</tt><big>(</big><big>)</big><a class="headerlink" href="#readline.get_completer" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the completer function, or <tt class="xref docutils literal"><span class="pre">None</span></tt> if no completer function has been set.</p>
<p>
<span class="versionmodified">New in version 2.3.</span></p>
</dd></dl>

<dl class="function">
<dt id="readline.get_completion_type">
<tt class="descclassname">readline.</tt><tt class="descname">get_completion_type</tt><big>(</big><big>)</big><a class="headerlink" href="#readline.get_completion_type" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the type of completion being attempted.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<dl class="function">
<dt id="readline.get_begidx">
<tt class="descclassname">readline.</tt><tt class="descname">get_begidx</tt><big>(</big><big>)</big><a class="headerlink" href="#readline.get_begidx" title="Permalink to this definition">¶</a></dt>
<dd>Get the beginning index of the readline tab-completion scope.</dd></dl>

<dl class="function">
<dt id="readline.get_endidx">
<tt class="descclassname">readline.</tt><tt class="descname">get_endidx</tt><big>(</big><big>)</big><a class="headerlink" href="#readline.get_endidx" title="Permalink to this definition">¶</a></dt>
<dd>Get the ending index of the readline tab-completion scope.</dd></dl>

<dl class="function">
<dt id="readline.set_completer_delims">
<tt class="descclassname">readline.</tt><tt class="descname">set_completer_delims</tt><big>(</big><em>string</em><big>)</big><a class="headerlink" href="#readline.set_completer_delims" title="Permalink to this definition">¶</a></dt>
<dd>Set the readline word delimiters for tab-completion.</dd></dl>

<dl class="function">
<dt id="readline.get_completer_delims">
<tt class="descclassname">readline.</tt><tt class="descname">get_completer_delims</tt><big>(</big><big>)</big><a class="headerlink" href="#readline.get_completer_delims" title="Permalink to this definition">¶</a></dt>
<dd>Get the readline word delimiters for tab-completion.</dd></dl>

<dl class="function">
<dt id="readline.set_completion_display_matches_hook">
<tt class="descclassname">readline.</tt><tt class="descname">set_completion_display_matches_hook</tt><big>(</big><span class="optional">[</span><em>function</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#readline.set_completion_display_matches_hook" title="Permalink to this definition">¶</a></dt>
<dd><p>Set or remove the completion display function.  If <em>function</em> is
specified, it will be used as the new completion display function;
if omitted or <tt class="xref docutils literal"><span class="pre">None</span></tt>, any completion display function already
installed is removed.  The completion display function is called as
<tt class="docutils literal"><span class="pre">function(substitution,</span> <span class="pre">[matches],</span> <span class="pre">longest_match_length)</span></tt> once
each time matches need to be displayed.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<dl class="function">
<dt id="readline.add_history">
<tt class="descclassname">readline.</tt><tt class="descname">add_history</tt><big>(</big><em>line</em><big>)</big><a class="headerlink" href="#readline.add_history" title="Permalink to this definition">¶</a></dt>
<dd>Append a line to the history buffer, as if it was the last line typed.</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a title="Python identifier completion, suitable for the GNU readline library." class="reference external" href="rlcompleter.html#module-rlcompleter"><tt class="xref docutils literal"><span class="pre">rlcompleter</span></tt></a></dt>
<dd>Completion of Python identifiers at the interactive prompt.</dd>
</dl>
</div>
<div class="section" id="example">
<span id="readline-example"></span><h2>17.8.1. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates how to use the <tt class="xref docutils literal"><span class="pre">readline</span></tt> module&#8217;s
history reading and writing functions to automatically load and save a history
file named <tt class="docutils literal"><span class="pre">.pyhist</span></tt> from the user&#8217;s home directory.  The code below would
normally be executed automatically during interactive sessions from the user&#8217;s
<span class="target" id="index-502"></span><a class="reference external" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><strong class="xref">PYTHONSTARTUP</strong></a> file.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span>
<span class="n">histfile</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">&quot;HOME&quot;</span><span class="p">],</span> <span class="s">&quot;.pyhist&quot;</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">readline</span><span class="o">.</span><span class="n">read_history_file</span><span class="p">(</span><span class="n">histfile</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
    <span class="k">pass</span>
<span class="kn">import</span> <span class="nn">atexit</span>
<span class="n">atexit</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">readline</span><span class="o">.</span><span class="n">write_history_file</span><span class="p">,</span> <span class="n">histfile</span><span class="p">)</span>
<span class="k">del</span> <span class="n">os</span><span class="p">,</span> <span class="n">histfile</span>
</pre></div>
</div>
<p>The following example extends the <a title="code.InteractiveConsole" class="reference external" href="code.html#code.InteractiveConsole"><tt class="xref docutils literal"><span class="pre">code.InteractiveConsole</span></tt></a> class to
support history save/restore.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">code</span>
<span class="kn">import</span> <span class="nn">readline</span>
<span class="kn">import</span> <span class="nn">atexit</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="k">class</span> <span class="nc">HistoryConsole</span><span class="p">(</span><span class="n">code</span><span class="o">.</span><span class="n">InteractiveConsole</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">locals</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s">&quot;&lt;console&gt;&quot;</span><span class="p">,</span>
                 <span class="n">histfile</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">expanduser</span><span class="p">(</span><span class="s">&quot;~/.console-history&quot;</span><span class="p">)):</span>
        <span class="n">code</span><span class="o">.</span><span class="n">InteractiveConsole</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">init_history</span><span class="p">(</span><span class="n">histfile</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">init_history</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">histfile</span><span class="p">):</span>
        <span class="n">readline</span><span class="o">.</span><span class="n">parse_and_bind</span><span class="p">(</span><span class="s">&quot;tab: complete&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">readline</span><span class="p">,</span> <span class="s">&quot;read_history_file&quot;</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">readline</span><span class="o">.</span><span class="n">read_history_file</span><span class="p">(</span><span class="n">histfile</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="n">atexit</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">save_history</span><span class="p">,</span> <span class="n">histfile</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">save_history</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">histfile</span><span class="p">):</span>
        <span class="n">readline</span><span class="o">.</span><span class="n">write_history_file</span><span class="p">(</span><span class="n">histfile</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">17.8. <tt class="docutils literal"><span class="pre">readline</span></tt> &#8212; GNU readline interface</a><ul>
<li><a class="reference external" href="#example">17.8.1. Example</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="mmap.html"
                                  title="previous chapter">17.7. <tt class="docutils literal docutils literal docutils literal"><span class="pre">mmap</span></tt> &#8212; Memory-mapped file support</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="rlcompleter.html"
                                  title="next chapter">17.9. <tt class="docutils literal docutils literal"><span class="pre">rlcompleter</span></tt> &#8212; Completion function for GNU readline</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/readline.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="rlcompleter.html" title="17.9. rlcompleter — Completion function for GNU readline"
             >next</a> |</li>
        <li class="right" >
          <a href="mmap.html" title="17.7. mmap — Memory-mapped file support"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="someos.html" >17. Optional Operating System Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>